package org.garbagecan.quartzstudy.cron;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.CronCalendar;

public class TestTrigger {
	public static void main(String[] args) throws Exception {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler scheduler = sf.getScheduler();
		
		JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myjob","mygroup").build();
		
		Trigger trigger = TriggerBuilder.newTrigger()
				.withIdentity("mytrigger", "mygroup")
				.forJob(jobDetail)
				.startNow()
				.withPriority(5)
				.withSchedule(CronScheduleBuilder
					.cronSchedule("0/2 * * * * ?"))
				.build();

		scheduler.scheduleJob(jobDetail, trigger);

		scheduler.start();
		Thread.sleep(10 * 60 * 1000);
		scheduler.shutdown();
	}

	public static Date parseToDate(String s) throws ParseException {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = df.parse(s);
		return date;
	}
}
